package com.leruan.aasimp.dao.impl;

import java.util.List;
import java.util.Map;


import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.leruan.aasimp.constant.UsedCarConstant;
import com.leruan.aasimp.dao.CarInsuranceDao;
import com.leruan.aasimp.entity.CarInsurance;

public class CarInsuranceDaoImpl extends HibernateDaoSupport implements CarInsuranceDao{
	public List<CarInsurance> findAll(){
		Session session=this.getSession();
		String hql="from CarInsurance";
		Query query=session.createQuery(hql);
		List<CarInsurance> results=query.list();
		return results;
	}

	public void add(CarInsurance carInsurance) {
		Session session = this.getSession();
		session.save(carInsurance);
	}

	public CarInsurance findById(long findById) {
		Session session=this.getSession();
		String hql="from CarInsurance where idInsurance=?";
		return (CarInsurance) session.createQuery(hql).setLong(0, findById).uniqueResult();
	}

	public void update(CarInsurance carInsurance) {
		getHibernateTemplate().update(carInsurance);
	}

	public void delete(CarInsurance carInsurance) {
		getHibernateTemplate().delete(carInsurance);
	}

	public List<CarInsurance> getByList(Map<String, String> map, int pageNo) {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("from CarInsurance where 1 = 1 ");
		if (map.get("userid") != null)
			sb.append(" and userid=:userid ");
		if (map.get("idCompany") != null)
			sb.append(" and idCompany=:idCompany ");
		Query query = session.createQuery(sb.toString());

		if (map.get("userid") != null)
			query.setString("userid", map.get("userid"));
		if (map.get("idCompany") != null)
			query.setLong("idCompany", Long.parseLong(map.get("idCompany")));
		
		int first = (pageNo - 1) * UsedCarConstant.PAGE_SIZE;
		query.setFirstResult(first);
		query.setMaxResults(UsedCarConstant.PAGE_SIZE);
		List<CarInsurance> results=query.list();
		return results;
	}

	public Long getTotal(Map<String, String> map) {
		Session session = this.getSession();
		StringBuffer sb = new StringBuffer("select count(idInsurance) from CarInsurance where 1 = 1 ");
		if (map.get("userid") != null)
			sb.append(" and userid=:userid ");
		if (map.get("idCompany") != null)
			sb.append(" and idCompany=:idCompany ");
		Query query = session.createQuery(sb.toString());

		if (map.get("userid") != null)
			query.setString("userid", map.get("userid"));
		if (map.get("idCompany") != null)
			query.setLong("idCompany", Long.parseLong(map.get("idCompany")));
		
		return (Long)query.uniqueResult();
	}

	public CarInsurance findByUserId(long parseLong) {
		Session session=this.getSession();
		String hql="from CarInsurance where UserId=?";
		return (CarInsurance) session.createQuery(hql).setLong(0, parseLong).uniqueResult();
	}

	public CarInsurance findByUserId(String id, String parseLong) {
		Session session=this.getSession();
		String hql="from CarInsurance where userid=? and idCar=? and type=2";
		return (CarInsurance) session.createQuery(hql).setString(0, id).setString(1, parseLong).uniqueResult();
	}
}
